package cn.netjava.robo3d.actions;


import cn.netjava.robo3d.RoboTank;

import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyInputAction;
import com.jme.math.FastMath;
import com.jme.math.Matrix3f;
import com.jme.math.Quaternion;
import com.jme.math.Vector3f;

public class LeftRightAction extends KeyInputAction {

	public static int left=1;
	public static int right=-1;
	private int direction;
	private float turnspeed;
	private Quaternion carqua;
    private Vector3f upAxis=new Vector3f(0,1,0) ;
	 //temporary variables to handle rotation
    //the node to manipulate
    private RoboTank car;
    //an optional axis to lock, preventing rolling on this axis.

    /**
     * Constructor instantiates a new <code>KeyNodeRotateLeftAction</code>
     * object using the node and speed parameters for it's attributes.
     *
     * @param node
     *            the node that will be affected by this action.
     * @param speed
     *            the speed at which the node can move.
     */
    public LeftRightAction(RoboTank car,int direction) {
    	this.direction=direction;
        this.car = car;
        this.turnspeed = 1.0f;
        carqua=new Quaternion();
    }


    /**
     * <code>performAction</code> rotates the camera about it's up vector or
     * lock axis at a speed of movement speed * time. Where time is the time
     * between frames and 1 corresponds to 1 second.
     *
     * @see com.jme.input.action.KeyInputAction#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent evt) {
        //we want to turn differently depending on which direction we are traveling in.
//    	if(Math.abs(car.getspeed())>0.2f){ //没有速度,就不让它整体转动了
    	   float step=0;
//		        if(car.getspeed() < 0) {
//		        	step=-direction*turnspeed*evt.getTime();		        	
//		        } else {
		        	 step=direction*turnspeed*evt.getTime();
//		        }
//		        upAxis=car.getCardirection().cross(car.getLeftdirection()).normalize();  //绕法线旋转
//		        car.setLocks(2);
		        upAxis=car.getNormal();	 
                car.addAlpha(step);
		        carqua.fromAngleNormalAxis(car.getAlpha(), upAxis);
		        car.setLocalRotation(carqua);
				car.rotateUpTo(upAxis);

		        car.getLocalRotation().normalize();
		        
//    	}
    }
}
